<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="800px"
            :custom-class="popupCustomClass"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="文章标题" prop="title">
                            <el-input v-model="formData.title" clearable placeholder="请输入文章标题" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="作者" prop="author">
                            <el-input v-model="formData.author" clearable placeholder="请输入作者" />
                        </el-form-item>
                    </el-col>
                </el-row>
                
                <el-form-item label="文章摘要" prop="summary">
                    <el-input 
                        v-model="formData.summary" 
                        type="textarea" 
                        :rows="3" 
                        clearable 
                        placeholder="请输入文章摘要" 
                    />
                </el-form-item>
                
                <el-form-item label="封面图片" prop="cover">
                    <material-picker v-model="formData.cover" :limit="1" />
                </el-form-item>
                
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="formData.status">
                                <el-radio :label="1">正常</el-radio>
                                <el-radio :label="0">禁用</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="排序" prop="sort">
                            <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        </el-form-item>
                    </el-col>
                </el-row>
                
                <el-form-item label="文章内容" prop="content">
                    <editor v-model="formData.content" height="400px" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="articleEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import Editor from '@/components/editor/index.vue'
import MaterialPicker from '@/components/material/picker.vue'
import { apiMuArticleAdd, apiMuArticleEdit, apiMuArticleDetail } from '@/api/graveyardManage/article'
import { reactive, ref, shallowRef, computed, nextTick } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑文章' : '新增文章'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    author: '',
    summary: '',
    content: '',
    cover: '',
    status: 1,
    sort: 0
})

// 表单验证
const formRules = reactive<any>({
    title: [{
        required: true,
        message: '请输入文章标题',
        trigger: ['blur']
    }],
    author: [{
        required: true,
        message: '请输入作者',
        trigger: ['blur']
    }],
    content: [{
        required: true,
        message: '请输入文章内容',
        trigger: ['blur']
    }]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiMuArticleDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' 
        ? await apiMuArticleEdit(data) 
        : await apiMuArticleAdd(data)
    popupRef.value?.close()
    emit('success')
}

// 打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    // 重置表单数据
    Object.assign(formData, {
        id: '',
        title: '',
        author: '',
        summary: '',
        content: '',
        cover: '',
        status: 1,
        sort: 0
    })
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>

<style scoped>
.edit-popup :deep(.el-dialog) {
    max-height: 90vh;
    overflow-y: auto;
}
</style>